﻿using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace VaccinationHost.Services
{
    /// <summary>
    /// JWT对称加密服务
    /// </summary>
    public class HSJWTService : IJWTService
    {
        #region Option注入
        #endregion
        /// <summary>
        /// 获取token
        /// </summary>
        /// <param name="acct"></param>
        /// <returns></returns>
        public string GetToken(string openId, string userId, string name)
        {
            var claims = new[]
            {
                new Claim(type:System.Security.Claims.ClaimTypes.Name, $"{openId}|_|{userId}|_|{name}"),
                 //new Claim("openId", openId),
                 //new Claim("userId", userId),
                 //new Claim("name", name),
            };

            //需要加密：需要加密key:
            SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("MIGfMA0GDE49f4qGSIb3DQEBAQUAA4NADCBiQKBgQDI2a2EJ7m872v0fyoSDJT2o1+SitIeJSWtLJU8/Wz2m7gStexaseDR2keD+Lka6DSTy8gt9UwfgVQo6uKjVLG5Ex7PiGOODVqAEghBuS7JzIYU5RvI54nNDAPfnJsas96mSA7L/D7RTE2drj6hfoZjJpMPZUQI/B1Qjb5H3K3NwIDAQAB"));
            SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            JwtSecurityToken token = new JwtSecurityToken(
             issuer: "ssss",
             audience: "ssss",
             claims: claims,
             expires: DateTime.Now.AddMinutes(720),
             signingCredentials: creds);
            string returnToken = new JwtSecurityTokenHandler().WriteToken(token);
            return returnToken;
        }
    }
}
